if (!Tapestry.ZoneRefresh) {
	Tapestry.ZoneRefresh = {};
}

Tapestry.Initializer.zoneRefresh = function(params) {
	// Ensure a valid period. Not required as PeriodicalUpdater already takes
	// care of it
	// but will will skip unnecessary steps
	if (params.period <= 0) {
		return;
	}

	// If the timer is already present, don't create a new one
	if (Tapestry.ZoneRefresh[params.id]) {
		// Timer already in use
		return;
	}

	// Set zone

	// Function to be called for each refresh
	var keepUpdatingZone = function(e) {
		try {
			var zoneManager = Tapestry.findZoneManagerForZone(params.id);
			zoneManager.updateFromURL(params.URL);
		} catch (e) {
			e.stop();
			Tapestry.error(Tapestry.Messages.invocationException, {
				fname : "Tapestry.Initializer.zoneRefresh",
				params : params,
				exception : e
			});
		}
	};

	// Create and store the executor (prototype.js)
	Tapestry.ZoneRefresh[params.id] = new PeriodicalExecuter(keepUpdatingZone,
			params.period);
};

// Before unload clear all the timers
Event.observe(window, "beforeunload", function() {
	if (Tapestry.ZoneRefresh) {
		for ( var propertyName in Tapestry.ZoneRefresh) {
			var property = Tapestry.ZoneRefresh[propertyName];
			property.stop();
		}
	}
});